package com.flejay.dreamrecruiter.tools.converter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.springframework.util.StringUtils;

import com.flejay.dreamrecruiter.model.Block;
import com.flejay.dreamrecruiter.model.SubBlock;
import com.flejay.dreamrecruiter.tools.IConstantsUtil;
import com.flejay.dreamrecruiter.xsd.BlockXML;
import com.flejay.dreamrecruiter.xsd.BlocksXML;
import com.flejay.dreamrecruiter.xsd.CV;
import com.flejay.dreamrecruiter.xsd.SubBlockXML;

/**
 * 
 * @author Victor Fleurant
 *
 */
@SuppressWarnings("restriction")
public final class XmlBlocksTool {

	private XmlBlocksTool(){
		super();
	}
	/**
	 * Write on Hard Drive in the specified fileName the blocks passed in parameter
	 * 
	 * @param blocks
	 */
	public static CV convertCVIntoXml(final List<Block> blocks){
		CV cv = new CV();
		cv.setBlocks(new BlocksXML());
		cv.getBlocks().getBlock().addAll(fillBlockList(blocks));
		return cv;
	}
	
	/**
	 * Write CV into Hard Drive
	 * 
	 * @param cv
	 * @param fileName
	 * @throws JAXBException
	 */
	public static void saveCVOnHardDrive(CV cv, String fileName) throws JAXBException{
		JAXBContext jaxbContext = JAXBContext.newInstance(CV.class);
		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
		// output pretty printed
		jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		// writing file
		File file = new File(fileName);
		jaxbMarshaller.marshal(cv,file);
	}
	
	/**
	 * Fill block data
	 * 
	 * @param blockList
	 * @return
	 */
	public static List<BlockXML> fillBlockList(List<Block> blockList){
		List<BlockXML> res = new ArrayList<BlockXML>();
		for (Block block : blockList){
			BlockXML xmlBlock = new BlockXML();
			xmlBlock.getSubBlock().addAll(fillSubBlockList(block.getSubBlockList()));
			xmlBlock.setTag(block.getLabel());
			xmlBlock.setContent(block.getStringContent());
			xmlBlock.setOverSized(block.isOverSized());
			res.add(xmlBlock);
		}
		return res;
	}
	
	/**
	 * Fill sub block data
	 * 
	 * @param subBlockList
	 * @return
	 */
	public static List<SubBlockXML> fillSubBlockList(List<SubBlock> subBlockList){
		List<SubBlockXML> res = new ArrayList<SubBlockXML>();
		if (subBlockList != null && !subBlockList.isEmpty()){
			for(SubBlock subBlock : subBlockList){
				SubBlockXML subBlockXml = new SubBlockXML();
				if("mission".equalsIgnoreCase(subBlock.getAssociatedTag().getLabel())){
					//si c'est la premiere ligne alors c'est le titre
					String[] lines = subBlock.getStringContent().split(IConstantsUtil.LINE_SEPARATOR);
					if (lines.length > 0){
						subBlockXml.setTitle(lines[0]);
					}
				}
				subBlockXml.setContent(subBlock.getStringContent());
				subBlockXml.setTag(subBlock.getAssociatedTag().getLabel());
				if (!StringUtils.isEmpty(subBlock.getStartDate())){
					subBlockXml.setStartDate(subBlock.getStartDate());
				}
				if (!StringUtils.isEmpty(subBlock.getEndDate())){
					subBlockXml.setEndDate(subBlock.getEndDate());
				}
				res.add(subBlockXml);
			}	
		}
		return res;
	}
}
